package com.tanhua.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.api.mongo.PublishApi;
import com.tanhua.api.mongo.VideoApi;
import com.tanhua.db.UserInfo;
import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Publish;
import com.tanhua.mongo.Video;
import com.tanhua.vo.*;
import org.apache.dubbo.config.annotation.Reference;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Reference
    private UserInfoApi userInfoApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private VideoApi videoApi;
    @Reference
    private PublishApi publishApi;
    @Autowired
    private UserService userService;

    public ResponseEntity findUsers(Integer page, Integer pagesize) {
        IPage<UserInfo> iPage = userInfoApi.findPage(page, pagesize);
        PageBeanVo pageBeanVo = new PageBeanVo(page, pagesize, iPage.getTotal(), iPage.getRecords());
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findByUserId(Long userId) {
        UserInfo userInfo = userInfoApi.findByUserId(userId);
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);
        vo.setAge(userInfo.getAge() + "");
        vo.setCreated(userInfo.getCreated());
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity findUserVideo(Integer page, Integer pagesize, Long uid) {
        PageBeanVo pageBeanVo = videoApi.findUserVideo(page, pagesize, uid);
        List<Video> items = (List<Video>) pageBeanVo.getItems();
        List<VideoVo> videoVoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(items)) {
            for (Video video : items) {
                UserInfo userInfo = userInfoApi.findByUserId(video.getUserId());
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(video, videoVo);
                BeanUtils.copyProperties(userInfo, videoVo);

                videoVo.setSignature(video.getText());
                videoVo.setCover(video.getPicUrl());
                videoVo.setId(video.getId().toString());
                videoVoList.add(videoVo);
            }
        }
        pageBeanVo.setItems(videoVoList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMessages(Integer page, Integer pagesize, Long uid, Integer state) {
        PageBeanVo pageBeanVo = publishApi.findMessages(page, pagesize, uid, state);
        List<Publish> publishList = (List<Publish>) pageBeanVo.getItems();
        List<PublishVo> publishVoList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(publishList)) {
            for (Publish publish : publishList) {
                UserInfo userInfo = userInfoApi.findByUserId(publish.getUserId());
                PublishVo vo = new PublishVo();
                vo.setUserInfo(userInfo);
                vo.setPublish(publish);
                vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));
                vo.setId(publish.getId().toString());
                publishVoList.add(vo);
            }
        }

        pageBeanVo.setItems(publishVoList);

        return ResponseEntity.ok(pageBeanVo);

    }

    public ResponseEntity findPublishInfo(String publishId) {
        Publish publish = publishApi.findPublishById(publishId);
        UserInfo userInfo = null;
        if (publish != null) {
            userInfo = userInfoApi.findByUserId(publish.getUserId());
        }

        PublishVo vo = new PublishVo();
        vo.setUserInfo(userInfo);
        vo.setPublish(publish);
        vo.setId(publish.getId().toString());
        vo.setUserId(userInfo.getId());
        vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm"));
        return ResponseEntity.ok(vo);

    }

    public ResponseEntity findPublishComment(Integer page, Integer pagesize, String messageID) {
        PageBeanVo pageBeanVo = commentApi.findCommentByPage(page, pagesize, messageID);
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        ArrayList<CommentVo> voList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(items)) {
            for (Comment comment : items) {
                UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());
                CommentVo vo = new CommentVo();
                vo.setNickname(userInfo.getNickname());
                BeanUtils.copyProperties(comment, vo);
                vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm"));
                vo.setId(comment.getId().toString());
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

}
